/*****************************************************************************//*!
* file  : systask.c
* creat : apleilx
* note :  除初始化外，禁止该任务及其子程序使用含有阻塞的功能，包括数据存储，USB操作等等
* data  : 2019/07/02
*******************************************************************************/
#define _SYSTASK_MODULE_
#include "bsp.h"
#include "bsp_spim.h"
#include "os_obj.h"
#include "pincfg.h"
#include "systask.h"
#include "nor_cmd.h"
#include "stdio.h"
#include "sys_com.h"
#include "string.h"
#include "stdlib.h"
#include "common.h"
#include "sys_malloc.h"
#include "sys_init.h"
#include "nor_cmd.h"

void sys_init(void);
void eep_iic_init(void);
void dma_test(void);
void spim_sw_spi_init(void);
void spi_nor_test(void);

/*****************************************************************************//*!
* @brief   storage task.
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void system_task(void const *argument)
{  
    thread_create();
    
	app_tick_resume();
	
    dbg_print("system start\n");
    
    pulse_init(&sys_st.pulse, app_tick_get());
    
	sys_st.buff = mem_sram.api->malloc(1024);
	
	memset(sys_st.buff, 0, 126);
	
	FLASH_InitExtFlash();
	//spim_sw_spi_init();
	
	sys_st.init.global = 1;
	
    for(;;){
        
        osDelay(ms_ticks(4));
        
        pulse_create(&sys_st.pulse, app_tick_get());
		
        if(sys_st.pulse.bPulse_100ms)
        {
            uint8_t buff[25];
			
            TimeRun(&sys_st.sw_rtc);
			
            sys_st.sig_rtc = time_to_sig(&sys_st.sw_rtc);
			
            TimeToAscii(&sys_st.sw_rtc,buff);
        }
		
		if(sys_st.task_info)
		{
			sys_st.task_info = 0;
			os_task_info();
		}
		
		sys_st.info.stack_used = stack_used();
		
		if((sys_st.pulse.AutoCnt & 0xF) < 1)
		{
			o_led2.en();
		}
		else
		{
			o_led2.dis();
		}
		
		if(sys_st.pulse.bPulse_1s)
		{
			o_led1.tog();
		}
		
		if(sys_st.cmd.copy_sd_to_afat)
		{
			sys_st.cmd.copy_sd_to_afat = 0;
			spi_nor_test();
		}
    }
}

/*****************************************************************************
* @brief   sys init.
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_init(void)
{
	
}

uint32_t rt_tick_get(void)
{
    return sys_st.ticks;
}

/*****************************************************************************
* @brief   sys init.
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void spi_nor_test(void)
{
	uu8 * buff;
	uu32 tickn;
	
	dbg_print("spi nor test start !\n");
	
	buff = malloc(4096);
	
	
	loop_n(32)
		buff[index] = index;
	
	nor_sector_erase(0x800000);
	
	nor_write(0x800000, 32, buff);
	dbg_u8_print("spi write: ", buff, 32, 40);
	
	memset(buff, 0, 64);
	nor_read(0x800000, 32, buff);
	dbg_u8_print("spi read : ", buff, 32, 40);
	
	memset(buff, 0, 64);
	nor_qa_read(0x800000, 32, buff);
	dbg_u8_print("qpi read : ", buff, 32, 40);
	
	loop_n(32)
	buff[index] = 3*index;
	
	nor_q_write(0x800100, 32, buff);
	dbg_u8_print("qpi write: ", buff, 32, 40);
	
	memset(buff, 0, 64);
	nor_read(0x800100, 32, buff);
	dbg_u8_print("spi read : ", buff, 32, 40);
	
	memset(buff, 0, 64);
	nor_qa_read(0x800100, 32, buff);
	dbg_u8_print("qpa read : ", buff, 32, 40);
	
	memset(buff, 0, 64);
	nor_q_read(0x800100, 32, buff);
	dbg_u8_print("qpi read : ", buff, 32, 40);
	
	/*
	dbg_print("qread start @ %d \n", app_tick_get());
	
	tickn = app_tick_get();
	
	for(int cnt = 0; cnt < 2048; cnt ++)
	{
		nor_q_read(0x800100, 4096, buff);
	}
	dbg_print("qread time used: @ %d \n", app_tick_get() - tickn);
	*/
	
	free(buff);
}
